package site.wetsion.galio.sdk.filter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import org.springframework.web.filter.GenericFilterBean;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * 全局的filter，主要用于为请求设置上当前uri
 *
 * order 1
 *
 * @author weixin
 * @version 1.0
 * @CLassName GalioGlobalFilter
 * @date 2019/3/13 4:36 PM
 */
public class GalioGlobalFilter extends GenericFilterBean {

    private final static Logger logger = LoggerFactory.getLogger(GalioGlobalFilter.class);

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        logger.debug("entered global filter");
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        String currentUrl = httpServletRequest.getRequestURL().toString();
        if (!StringUtils.isEmpty(httpServletRequest.getQueryString())) {
            currentUrl += httpServletRequest.getQueryString();
        }
        request.setAttribute("currentUri", currentUrl);
        chain.doFilter(request, response);
    }
}
